//
//  TUISystemMessageCell.m
//  UIKit
//
//  Created by annidyfeng on 2019/5/30.
//

#import "LGSystemMessageCell.h"
#import "TUIDefine.h"

@interface LGSystemMessageCell()

@property (nonatomic, strong) UILabel *timeLabel;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;
@property (nonatomic , strong)NSDateFormatter *weekformatter;
@property (nonatomic , strong)NSDateFormatter *hourformatter;

@end

@implementation LGSystemMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.timeLabel];
    }
    return self;
}
-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    CGFloat topMargin = [LGNSString(dataDic[@"showTime"]) isEqualToString:@"Y"]?20:0;
    self.timeLabel.hidden = [LGNSString(dataDic[@"showTime"]) isEqualToString:@"Y"]?NO:YES;
    self.timeLabel.text = [self messageTime:[dataDic[@"message_time"] integerValue]];
    
    
}

//获取时间
-(NSString *)messageTime:(NSInteger)timestamp{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDate *todayStart = [self startOfDayForDate:[NSDate date]];
    NSDate *yesterdayStart = [self startOfDayForDate:[NSDate dateWithTimeIntervalSinceNow:-24 * 60 * 60]];
    NSDate *dayBeforeYesterdayStart = [self startOfDayForDate:[NSDate dateWithTimeIntervalSinceNow:-2 * 24 * 60 * 60]];

    // 比较日期部分（忽略时间）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *todayComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:todayStart];
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];

    BOOL isToday = [todayComponents isEqual:dateComponents];
    
    BOOL isYesterday = [[calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:yesterdayStart] isEqual:dateComponents];
    
    BOOL isDayBeforeYesterday = [[calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:dayBeforeYesterdayStart] isEqual:dateComponents];
    
    // 判断是否本周（周一 00:00:00 ~ 周日 23:59:59）
    BOOL isThisWeek = [self isThisWeek:date];

    if (isToday) {
        return [self.hourformatter stringFromDate:date];
        
    } else if (isYesterday) {
        NSLog(@"昨天");
        return [NSString stringWithFormat:@"昨天 %@",[self.hourformatter stringFromDate:date]];
        
    } else if (isDayBeforeYesterday) {
        NSLog(@"前天");
        return [NSString stringWithFormat:@"前天 %@",[self.hourformatter stringFromDate:date]];
        
    } else if (isThisWeek) {
        NSLog(@"本周（非今天/昨天/前天）");
        NSString *key = [self.weekformatter stringFromDate:date];
        return [NSString stringWithFormat:@"%@ %@",[self.weekformatter stringFromDate:date],[self.hourformatter stringFromDate:date]];;
        
    } else {
        NSLog(@"更早的日期");
        return [self.formatter stringFromDate:date];
    }
}

- (NSDate *)startOfDayForDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];
    return [calendar dateFromComponents:components];
}

-(BOOL)isThisWeek:(NSDate *)date{
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2]; // 设置周一为一周的第一天（默认是周日）
    
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];
    
    // 计算本周的周一
    NSDate *startOfWeek = [calendar dateFromComponents:components];
    // 如果当前日期不是周一，则回退到上周一
    NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:today];
    NSInteger daysToSubtract = (weekdayComponents.weekday - calendar.firstWeekday + 7) % 7;
    startOfWeek = [calendar dateByAddingUnit:NSCalendarUnitDay value:-daysToSubtract toDate:startOfWeek options:0];
    
    // 计算本周的周日（+6天）
    NSDate *endOfWeek = [calendar dateByAddingUnit:NSCalendarUnitDay value:6 toDate:startOfWeek options:0];
    // 设置时间为 23:59:59
    NSDateComponents *endComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:endOfWeek];
    [endComponents setHour:23];
    [endComponents setMinute:59];
    [endComponents setSecond:59];
    endOfWeek = [calendar dateFromComponents:endComponents];
    
    BOOL isThisWeek = ([date compare:startOfWeek] != NSOrderedAscending) && ([date compare:endOfWeek] != NSOrderedDescending);
    return isThisWeek;
}


#pragma mark
#pragma mark ====> 创建控件
-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _timeLabel.frame = CGRectMake(0, viewPix(5), Screen_W, 20);
    }
    return _timeLabel;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _titleLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:0];
    }
    return _contentLabel;
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc]init];
        _formatter.dateFormat = @"yyyy/MM/dd HH:mm";
    }
    return _formatter;
}

-(NSDateFormatter *)hourformatter{
    if(!_hourformatter){
        _hourformatter = [[NSDateFormatter alloc]init];
        _hourformatter.dateFormat = @"HH:mm";
    }
    return _hourformatter;
}

-(NSDateFormatter *)weekformatter{
    if(!_weekformatter){
        _weekformatter = [[NSDateFormatter alloc]init];
        _weekformatter.dateFormat = @"E";
    }
    return _weekformatter;
}


@end
